﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business.Entities;
using Business.Logic;

namespace UI.Desktop
{
    public partial class MateriasD : MdiFormPadre
    {
        private List<Plan> _ListaPlanes;

        public List<Plan> ListaPlanes
        {
            get { return _ListaPlanes; }
            set { _ListaPlanes = value; }
        }

        #region Constructores

        public MateriasD()
        {
            InitializeComponent();
            this.dgvMaterias.AutoGenerateColumns = false;
        }

        #endregion

        #region Métodos
        
        public void Listar()
        {
            try
            {
                MateriaLogic ml = new MateriaLogic();
                this.dgvMaterias.DataSource = null;
                this.dgvMaterias.DataSource = ml.GetAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion

        #region Eventos

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbNuevo_Click(object sender, EventArgs e)
        {
            MateriasDesktop MateriaDesk = new MateriasDesktop(MateriasDesktop.ModoForm.Alta, ListaPlanes);
            MateriaDesk.ShowDialog();
            this.Listar();
        }

        private void tsbConsultar_Click(object sender, EventArgs e)
        {
            int i = ((Materias)this.dgvMaterias.SelectedRows[0].DataBoundItem).ID;
            MateriasDesktop MateriasDesk = new MateriasDesktop(i, UI.Desktop.MateriasDesktop.ModoForm.Consulta, ListaPlanes);
            MateriasDesk.ShowDialog();
            this.Listar();
        }

        private void tsbEditar_Click(object sender, EventArgs e)
        {
            //DataBoundItem: toma el contenido de la celda
            //SelectedRows[0]: toma la primera celda de la fila seleccionada
            //dgvUsuarios: es la tabla en pantalla
            //(Usuario): lo transforma en tipo usuario
            //.ID: devuelve el contenido del atributo ID
            int i = ((Materias)this.dgvMaterias.SelectedRows[0].DataBoundItem).ID;
            MateriasDesktop MateriasDesk = new MateriasDesktop(i, UI.Desktop.MateriasDesktop.ModoForm.Modifica, ListaPlanes);
            MateriasDesk.ShowDialog();
            this.Listar();
        }

        private void tsbEliminar_Click(object sender, EventArgs e)
        {
            int i = ((Materias)this.dgvMaterias.SelectedRows[0].DataBoundItem).ID;
            MateriasDesktop matDesk = new MateriasDesktop(i, MateriasDesktop.ModoForm.Baja, ListaPlanes);
            matDesk.ShowDialog();
            this.Listar();
        }

        private void Materias_Load(object sender, EventArgs e)
        {
            this.Listar();
            PlanesLogic Plogic = new PlanesLogic();
            ListaPlanes = Plogic.GetAll();
        }

        private void btnActualizar_Click(object sender, EventArgs e)
        {
            this.Listar();
        }

        #endregion

    }
}
